package com.infosys.newSimpleInvertedIndex.helper;

import com.infosys.newSimpleInvertedIndex.entities.Employee;
import lombok.NoArgsConstructor;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.index.*;
import org.apache.lucene.search.*;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@NoArgsConstructor
public class EmployeeLuceneHelper extends BaseLuceneHelper<Employee> {
    private static String indexPathPostFix = "/employeeIndex";

    @Override
    public void constructIndex(List<Employee> employees, boolean erase) throws IOException {
            // get path
            File indexFile = new File(System.getProperty("user.dir") + indexPath + indexPathPostFix);
            Directory indexDirectory = FSDirectory.open(Paths.get(indexFile.getAbsolutePath()));
            // construct writer
            IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
            IndexWriter indexWriter = new IndexWriter(indexDirectory, indexWriterConfig);
            if (erase){
                indexWriter.deleteAll();
            }
            for (Employee employee : employees) {
                org.apache.lucene.document.Document luceneDocument = new org.apache.lucene.document.Document();
                // add all fields in employee
                luceneDocument.add(new StringField("id", employee.getId(), Field.Store.YES));
                luceneDocument.add(new StringField("name", employee.getName(), Field.Store.YES));
                luceneDocument.add(new StringField("govId", employee.getGovId(), Field.Store.YES));
                luceneDocument.add(new StringField("department", employee.getDepartment(), Field.Store.YES));
                luceneDocument.add(new StringField("enrollDate", employee.getEnrollDate().toString(), Field.Store.YES));
                indexWriter.addDocument(luceneDocument);
            }
            indexWriter.close();
            System.out.println("Index Update Complete");
        }

    @Override
    public List<Employee> queryIndex(String field, String queryText) throws IOException{
            File indexFile = new File(System.getProperty("user.dir") + indexPath + indexPathPostFix);
            Directory directory = FSDirectory.open(Paths.get(indexFile.getAbsolutePath()));

            IndexReader indexReader = DirectoryReader.open(directory);
            IndexSearcher indexSearcher = new IndexSearcher(indexReader);
            // create QueryTerm for the indexing query
            Query luceneQuery = new TermQuery(new Term(field, queryText));
            TopDocs topDocs = indexSearcher.search(luceneQuery, Integer.MAX_VALUE);

            int totalMatches = (int)topDocs.totalHits.value;
            // document scores
            ScoreDoc[] scoreDocs = topDocs.scoreDocs;
            List<Employee> returnEmployees = new ArrayList<>();

            // traver the interesting
            for (ScoreDoc scoreDoc : scoreDocs) {
                org.apache.lucene.document.Document document = indexSearcher.doc(scoreDoc.doc);
                Employee employee = new Employee();
                employee.setId(document.get("id"));
                employee.setName(document.get("name"));
                employee.setGovId(document.get("govId"));
                employee.setDepartment(document.get("department"));
                employee.setEnrollDate(LocalDate.parse(document.get("enrollDate")));
                returnEmployees.add(employee);
            }
            indexReader.close();
            return returnEmployees;
        }
    }
